Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | import { NextResponse } from 'next/server' import { db, schema } from '@/db' import { eq } from 'drizzle-orm' import { getRoomMembers } from '@/lib/arcade/room-membership' import { createJoinRequest, getJoinRequest } from '@/lib/arcade/room-join-requests' import { withAuth } from '@/lib/auth/withAuth' import { getUserId } from '@/lib/viewer' import { getSocketIO } from '@/lib/socket-io' /** * POST /api/arcade/rooms/:roomId/join-request * Request to join an approval-only room * Body: * - userName: string */ export const POST = withAuth(async (request, { params }) => { try { const { roomId } = (await params) as { roomId: string } const userId = await getUserId() const body = await request.json() // Validate required fields if (!body.userName) { return NextResponse.json({ error: 'Missing required field: userName' }, { status: 400 }) } // Get room details const [room] = await db .select() .from(schema.arcadeRooms) .where(eq(schema.arcadeRooms.id, roomId)) .limit(1) if (!room) { return NextResponse.json({ error: 'Room not found' }, { status: 404 }) } // Check if room is approval-only if (room.accessMode !== 'approval-only') { return NextResponse.json( { error: 'This room does not require approval to join' }, { status: 400 } ) } // Check if user is already in the room const members = await getRoomMembers(roomId) const existingMember = members.find((m) => m.userId === userId) if (existingMember) { return NextResponse.json({ error: 'You are already in this room' }, { status: 400 }) } // Check if user already has a pending request const existingRequest = await getJoinRequest(roomId, userId) if (existingRequest && existingRequest.status === 'pending') { return NextResponse.json( { error: 'You already have a pending join request' }, { status: 400 } ) } // Create join request const joinRequest = await createJoinRequest({ roomId, userId: userId, userName: body.userName, }) // Broadcast to host via socket const io = await getSocketIO() if (io) { try { // Get host user ID const host = members.find((m) => m.isCreator) if (host) { io.to(`user:${host.userId}`).emit('join-request-received', { roomId, request: { id: joinRequest.id, userId: joinRequest.userId, userName: joinRequest.userName, requestedAt: joinRequest.requestedAt, }, }) } console.log(`[Join Request API] User ${userId} requested to join room ${roomId}`) } catch (socketError) { console.error('[Join Request API] Failed to broadcast request:', socketError) } } return NextResponse.json({ request: joinRequest }, { status: 200 }) } catch (error: any) { console.error('Failed to create join request:', error) return NextResponse.json({ error: 'Failed to create join request' }, { status: 500 }) } }) |